using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Events
{
    /// <summary>
    /// Represents a collection of System.ComponentModel.EventDescriptor objects.
    /// </summary>
    public class FrameXmlEventDescriptorCollection : EventDescriptorCollection, IList<EventDescriptor>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlEventDescriptorCollection"/> class.
        /// </summary>
        /// <param name="events">The events.</param>
        public FrameXmlEventDescriptorCollection(EventDescriptor[] events) : base(events)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlEventDescriptorCollection"/> class.
        /// </summary>
        /// <param name="events">An array of type <see cref="T:System.ComponentModel.EventDescriptor"/> that provides the events for this collection.</param>
        /// <param name="readOnly">true to specify a read-only collection; otherwise, false.</param>
        public FrameXmlEventDescriptorCollection(EventDescriptor[] events, bool readOnly) : base(events, readOnly)
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether [read only].
        /// </summary>
        /// <value><c>true</c> if [read only]; otherwise, <c>false</c>.</value>
        public bool ReadOnly { get; set; }

        #region ICollection<EventDescriptor> Members

        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public new void Add(EventDescriptor item)
        {
            base.Add(item);
        }

        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        /// <returns>
        /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public new bool Remove(EventDescriptor item)
        {
            int count = Count;
            base.Remove(item);
            return count > Count;
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.IList"/> is read-only.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Collections.IList"/> is read-only; otherwise, false.</returns>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// 	<paramref name="array"/> is null.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// 	<paramref name="arrayIndex"/> is less than 0.</exception>
        /// <exception cref="T:System.ArgumentException">
        /// 	<paramref name="array"/> is multidimensional.-or-<paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type <paramref name="T"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
        public void CopyTo(EventDescriptor[] array, int arrayIndex)
        {
            if (array == null) throw new ArgumentNullException("array");

            //array = new EventDescriptor[Count - arrayIndex];
            for (int index = arrayIndex; index < Count - arrayIndex; index++)
            {
                var element = this[index];
                array[index] = element;
            }
        }

        #endregion

        #region IEnumerable<EventDescriptor> Members

        ///<summary>
        ///Returns an enumerator that iterates through the collection.
        ///</summary>
        ///<returns>
        ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        ///</returns>
        IEnumerator<EventDescriptor> IEnumerable<EventDescriptor>.GetEnumerator()
        {
            return (IEnumerator<EventDescriptor>) GetEnumerator();
        }

        #endregion

        #region IList<EventDescriptor> Members

        ///<summary>
        ///Gets or sets the element at the specified index.
        ///</summary>
        ///
        ///<returns>
        ///The element at the specified index.
        ///</returns>
        ///
        ///<param name="index">The zero-based index of the element to get or set.</param>
        ///<exception cref="T:System.ArgumentOutOfRangeException">index is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"></see>.</exception>
        ///<exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IList`1"></see> is read-only.</exception>
        EventDescriptor IList<EventDescriptor>.this[int index]
        {
            get { return base[index]; }
            set
            {
                if (index >= Count && index < 0)
                    throw new ArgumentOutOfRangeException("index");

                RemoveAt(index);
                Insert(index, value);
            }
        }

        #endregion
    }
}